package crazymakercircle;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Author: DRevelation
 * @Date: 2021/7/27 15:31
 */
public class DrinkTea {

    public static final int SLEEP_GAP = 3;

    public static void main(String[] args) {
        CompletableFuture<Boolean> hotJob = CompletableFuture.supplyAsync(() -> {
            System.out.println("洗好茶壶");
            System.out.println("烧开水");
            try {
                TimeUnit.SECONDS.sleep(SLEEP_GAP);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("水开了");
            return true;
        });

        CompletableFuture<Boolean> washJob = CompletableFuture.supplyAsync(() -> {
            System.out.println("洗茶杯");
            try {
                TimeUnit.SECONDS.sleep(SLEEP_GAP);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("洗好了");
            return true;
        });

        CompletableFuture<String> drinkJob = hotJob.thenCombine(washJob, (hotOk, washOk) -> {
            if (hotOk && washOk) {
                System.out.println("泡茶喝，茶喝完");
                return "茶喝完了";
            }
            return "没有茶喝";
        });

        System.out.println(drinkJob.join());
    }
}
